<?php
declare (strict_types=1);

namespace app\course\model\base;

use app\common\model\BaseModel;
use app\course\model\video\CourseSectionVideo;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * 定义课程数据模型
 */
class CourseSection extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'course_section';
    /**
     * 隐藏课时数据
     *
     * @var [type]
     */
    protected $hidden = ['create_time', 'update_time', 'delete_time'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'title'       => 'string',
        'pid'         => 'integer',
        'course_id'   => 'integer',
        'is_charge'   => 'integer',
        'price'       => 'string',
        'status'      => 'integer',
        'create_time' => 'integer',
        'update_time' => 'integer',
        'start_time'  => 'integer',
        'end_time'    => 'integer',
    ];

    /**
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-17
     */
    protected function getStatusTextAttr($value = '', $data): string
    {
        return $data['status'] == 1 ? '正常' : '禁用';
    }

    /**
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-17
     */
    public function scopeStatus($query, $status = 1)
    {
        $query->where('status', $status);
    }

    public function scopeLastSec($query, $section_id)
    {
        $query->where('id', $section_id)->where('is_last', 1);
    }

    public function scopeCoLaSeSa($query, $course_id)
    {
        $query->whereCourseId($course_id)->whereIsLast(1)->whereStatus(1);
    }

    /**
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-17
     */
    public function searchCourseIdAttr($query, $value)
    {
        $query->where('course_id', $value);
    }

    /**
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-17
     */
    public function searchPidAttr($query, $value)
    {
        $query->where('pid', $value);
    }

    public function courseInfo(): BelongsTo
    {
        return $this->belongsTo(Course::class);
    }

    public function courseBaseInfo(): BelongsTo
    {
        return $this->belongsTo(Course::class)->bind(['course_title' => 'title', 'mhm_id', 'teacher_id']);
    }

    public function courseLock(): BelongsTo
    {
        return $this->belongsTo(Course::class)->bind(['course_lock' => 'lock']);
    }

    public function teacherId(): BelongsTo
    {
        return $this->belongsTo(Course::class)->bind(['teacher_id']);
    }

    public function liveType(): BelongsTo
    {
        return $this->belongsTo(Course::class)->bind(['live_type' => 'course_live_type']);
    }

    public function limitBuy(): BelongsTo
    {
        return $this->belongsTo(Course::class)->bind(['limit_buy' => 'limit_buy_count']);
    }

    public function dataInfo(): BelongsTo
    {
        return $this->belongsTo(CourseSectionVideo::class, 'course_data_id');
    }

    public function videoAttachStock(): BelongsTo
    {
        return $this->belongsTo(CourseSectionVideo::class, 'course_data_id')->with(['attach_stock'])->bind(['stock']);
    }

    public function mhmId(): BelongsTo
    {
        return $this->belongsTo(Course::class, 'course_id')->bind(['mhm_id']);
    }

    public function children(): HasMany
    {
        return $this->hasMany(CourseSection::class, 'pid');
    }

}
